<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property Post_model $post_model
 */
class Landing extends MX_Controller
{
    private $_langCode;

    function __construct()
    {
        
        parent::__construct();
        $this->load->library('Layout');
        $this->_langCode = $this->mycommon->getLangCode();
        session_start();
        unset($_SESSION['search']);
    }

    public function index($page='',$type='',$order='')
    {
        $data = array();
        $page = (intval($page) != 0) ? intval($page) : 1;
        $type = (intval($type) != 0) ? intval($type) : 1;
        $order = (intval($order) != 0) ? intval($order) : 1;

        if($type != 11 && $type != 10 && $type != 1) {
            $order = $type;
            $type = 1;
        }
        if($order != 2 && $order != 3 && $order != 1) {
            $type = $order;
            $order = 1;
        }

        $this->load->model('option/option_model');
        $this->load->model('category/category_model');
        $this->load->model('post/post_model');
        $this->load->model('list_network/list_network_model');
        $this->load->model('item_sim/item_sim_model');
        $this->load->model('list_group/list_group_model');
        $this->load->model('info/info_model');
        
        $tmpInfo = $this->list_network_model->getListNetwork();
        $listGroup = $this->list_group_model->getListGroup();
        $this->load->library('Pagination');
        $numPost = $this->item_sim_model->getNumAll();

        $config['base_url'] = base_url('trang-chu');
        
        $config['prefix'] = ($this->_langCode == 'vietnam') ? 'trang_' : 'page_';
        $config['suffix']= '';
        if($type == 1) {
            if($order==1) {
                $config['suffix'] .= '';
            }
            else $config['suffix'] .='_'.$order;
            
        } else {
            if($order==1) {
                $config['suffix'] .= '_'.$type;
                
            }
            else $config['suffix'] .='_'.$type.'_'.$order;
        }
        $config['suffix'] .= '.html';
        
        $config['total_rows'] = $numPost;
        
        $config['per_page'] = 50;

        $config['cur_page'] = $page;
        $config['use_page_numbers'] = TRUE;
        $config['first_link'] = '«';
        $config['last_link'] = '»';
        $config['cur_tag_open'] = '<a class="active-page">';
        $config['cur_tag_close'] = '</a>';
        $config['num_links'] = 6;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $start = ($page - 1) * $config['per_page'];
        $data['pagination'] = $this->pagination->create_links();
        //$tmpPost = $this->post_model->getPostByCatId($id, $config['per_page'], $start, $langCode);

        //$post = $this->item_sim_model->getAll($config['per_page'], $start);
        $post = $this->item_sim_model->getAllByType($config['per_page'], $start, $type, $order);
        $data['page'] = $page;
        $data['list_network'] = $tmpInfo;
        $data['list_group'] = $listGroup;
        $data['post'] = $post;
        $data['type'] = $type;
        $data['order'] = $order;
              
        $this->layout->view('landing/landing_view', $data);
    }
    
    public function change()
    {
        $data = array();
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    
    	$this->layout->view('landing/change_view', $data);
    }
    public function support()
    {
    	$id = 'tutorial';
    	$data = array();
    	$this->load->model('post/post_model');
    	
    	$info = $this->post_model->getContent($id);
    	print_r($info);
    	$this->layout->view('landing/support_view', $data);
    }
    public function info($id)
    {
        $data = array();
        $news = 'Hướng dẫn';
    	if($id == 1) {
    		$id = 'tutorial';
            $news = 'Hướng dẫn';
    	} else if($id == 2) {
    		$id = 'about';
            $news = 'Giới thiệu';
    	} else if($id == 3) {
    		$id = 'payment';
            $news = 'Chuyển khoàn';
    	} else if($id == 4) {
            
    		$id = 'contact';
            $news = 'Liên hệ';
            $data['frmToken1'] = uniqid();

            $this->session->set_userdata('frmToken1', $data['frmToken1']);
    	} else if($id == 5) {
    		$id = 'checkout';
            $news = 'Thanh Toán';
    	} else $id = 'tutorial';
    	
    	
    	$this->load->model('post/post_model');
        $this->load->model('info/info_model');
    	 
    	$info = $this->info_model->postInfo($id);
        
    	$data['info'] = $info[0];
    	$data['id'] = $id;
        $data['news'] = $news;
        if($id =='payment') {
            $this->layout->view('landing/change_view', $data);
        } else {
            $this->layout->view('landing/info_view', $data);
        }
    	
    }
    public function success_contact(){
        if($_POST) {
            $session_form = $_POST['session_form'];
            
            if($this->session->userdata('frmToken1') == $session_form) {
                $fullName = $_POST['full_name'];
                $emailContact = $_POST['email'];
                $phone = $_POST['phone'];
                $content = $_POST['content'];
                $this->load->model('post/post_model');
                
                $this->mycommon->loadMail();
                $postId = 'email';
                $tmpInfo = $this->post_model->postInfo($postId);
                $emailString = $tmpInfo[0]['content'];
                $emailArray = explode(',', $emailString);
                
                if(count($emailArray ==1)) {
                    $email = $emailArray[0];
                } else {
                    $email = $emailArray[0];
                    for($i = 1; $i<count($emailArray); $i ++) {
                        $this->phpmailer->AddCC($emailArray[$i]);
                    } 
                }
                //$email = 
                //$email ='nnthieps@gmail.com';
                
                $this->phpmailer->AddAddress($email);
                $this->phpmailer->Subject = "Liên hệ từ website SDT-$phone";
                //echo STATIC_DOMAIN;
                $body = "Có yêu cầu liên hệ với bạn trên website \n";
                $body.= "\t - Họ tên : $fullName\n";
                $body.= "\t - Email: $emailContact\n";
                
                $body.= "\t - Số sim điện thoại liên hệ : $phone\n";
                
                $body.= "\t - Nội dung : $content \n";
                
                
                $this->phpmailer->Body = $body;
                if ($this->phpmailer->Send()) {
                
                $error[] = 'Yêu cầu liên hệ đã được gửi thành công. Chúng tôi sẽ liên hệ lại với bạn sớm nhất, xin cám ơn!';
                
                } else {
                    $error[] = 'Yêu cầu liên hệ khi gửi có lỗi. Xin vui lòng thực hiện lại!';
                }
       
                
                    //add table require_sim
                //

                $array = array(
                        'sim_phone' =>  '',
                        'phone_contact'  =>  $phone,
                        'name_contact'  => $fullName,
                        //'time'  =>  gmdate('Y-m-d H:i:s', time() + 7*3600),
                        'address'  =>$emailContact,
                        'home_phone'  =>  '',
                        'method'  => '',
                        'note'  =>  $content,
                        'type'  =>  '3',//contact
                        'status'    =>  '1',
                         
                );
                $data['array'] = $array;
                $this->load->model('contact/contact_model');
                $id = $this->contact_model->addNew($array);
                 
                if($id) {
                    $this->session->unset_userdata('frmToken1');
                    $this->layout->view('landing/require_success', $data);
            
                }
            } else {
                 $url_redirect = site_url('thongtin/lien-he-4');
                 redirect($url_redirect);
            }
            
        }
    }
    public function about()
    {
    	$data = array();
    	$this->load->model('post/post_model');

    
    	$tmpInfo = $this->list_network_model->getListNetwork();

    
    	$this->layout->view('landing/about_view', $data);
    }
    public function sales()
    {
    	$data = array();
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    
    	$this->layout->view('landing/sales_view', $data);
    }
    public function contact()
    {
    	$data = array();
    	$this->load->model('option/option_model');
    	$this->load->model('category/category_model');
    	$this->load->model('post/post_model');
    	$this->load->model('list_network/list_network_model');
    	$this->load->model('item_sim/item_sim_model');
    	$this->load->model('list_group/list_group_model');
    
    	$tmpInfo = $this->list_network_model->getListNetwork();
    	$listGroup = $this->list_group_model->getListGroup();
    
    	$data['list_network'] = $tmpInfo;
    	$data['list_group'] = $listGroup;
    
    	$this->layout->view('landing/contact_view', $data);
    }
    //add sim save session
    public function addCard()
    {
    	session_start();
    	//session_unset();
    	$data = array();
    	$data['sim'] = array();
    	$data['gia'] = array();
    	if(isset($_SESSION['sim'])) {
    		$data['sim'] = $_SESSION['sim'];
    	}
    	if(isset($_SESSION['gia'])) {
    		$data['gia'] = $_SESSION['gia'];
    	}

    	if(isset($_GET['sim'])) {
    		$sim = $_GET['sim'];
    	}
    	if(isset($_GET['gia'])) {
    		$gia = $_GET['gia'];
    	}

    	if(count($data['sim'])>0) {
    		if(array_search($sim, $data['sim']) != NULL){
	    		$data['sim'][] = $sim;
	    		$data['gia'][] = $gia;
	    		session_unset();
	    		$_SESSION['sim']=$data['sim'];
	    		$_SESSION['gia']=$data['gia'];

    		} 
    			
    	}
    	//print_r($data);
    	$html = $this->load->view('landing/add_card_view', $data,true);
    	$return['status'] = 1;
    	$return['html'] = $html;

    	echo json_encode($return); exit;

    }

}
?>
